// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.world.logger;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import zombie.debug.DebugLog;
import zombie.network.GameClient;

public class WorldDictionaryLogger {
    private static final ArrayList<Log.BaseLog> _logItems = new ArrayList();

    public static void reset() {
        _logItems.clear();
    }

    public static void startLogging() {
        reset();
    }

    public static void log(Log.BaseLog log) {
        if (!GameClient.bClient) {
            _logItems.add(log);
        }
    }

    public static void log(String msg) {
        log(msg, true);
    }

    public static void log(String msg, boolean debugPrint) {
        if (!GameClient.bClient) {
            if (debugPrint) {
                DebugLog.log("WorldDictionary: " + msg);
            }

            _logItems.add(new Log.Comment(msg));
        }
    }

    public static void saveLog(String saveFile) throws IOException {
        if (!GameClient.bClient) {
            boolean _boolean = false;

            for (int int0 = 0; int0 < _logItems.size(); int0++) {
                Log.BaseLog baseLog0 = (Log.BaseLog)_logItems.get(int0);
                if (!baseLog0.isIgnoreSaveCheck()) {
                    _boolean = true;
                    break;
                }
            }

            if (_boolean) {
                File file0 = new File(zombie.ZomboidFileSystem.instance.getCurrentSaveDir() + File.separator);
                if (file0.exists() && file0.isDirectory()) {
                    String string = zombie.ZomboidFileSystem.instance.getFileNameInCurrentSave(saveFile);
                    File file1 = new File(string);

                    try {
                        FileWriter fileWriter = new FileWriter(file1, true);

                        try {
                            fileWriter.write("log = log or {};" + System.lineSeparator());
                            fileWriter.write("table.insert(log, {" + System.lineSeparator());

                            for (int int1 = 0; int1 < _logItems.size(); int1++) {
                                Log.BaseLog baseLog1 = (Log.BaseLog)_logItems.get(int1);
                                baseLog1.saveAsText(fileWriter, "\t");
                            }

                            fileWriter.write("};" + System.lineSeparator());
                        } catch (Throwable throwable0) {
                            try {
                                fileWriter.close();
                            } catch (Throwable throwable1) {
                                throwable0.addSuppressed(throwable1);
                            }

                            throw throwable0;
                        }

                        fileWriter.close();
                    } catch (Exception exception) {
                        exception.printStackTrace();
                        throw new IOException("Error saving WorldDictionary log.");
                    }
                }
            }
        }
    }
}
